﻿CREATE PROCEDURE [dbo].[yaf_rank_save](
                @RankID    INT,
                @BoardID   INT,
                @Name      NVARCHAR(50),
                @IsStart   BIT,
                @IsLadder  BIT,
                @MinPosts  INT,
                @RankImage NVARCHAR(50)  = NULL)
AS
    BEGIN
        DECLARE  @Flags INT
        IF @IsLadder = 0
        SET @MinPosts = NULL
        IF @IsLadder = 1
           AND @MinPosts IS NULL
        SET @MinPosts = 0
        SET @Flags = 0
        IF @IsStart <> 0
        SET @Flags = @Flags | 1
        IF @IsLadder <> 0
        SET @Flags = @Flags | 2
        IF @RankID > 0
        BEGIN
            UPDATE yaf_Rank
            SET    Name = @Name,
                   Flags = @Flags,
                   MinPosts = @MinPosts,
                   RankImage = @RankImage
            WHERE  RankID = @RankID
        END
        ELSE
        BEGIN
            INSERT INTO yaf_Rank
                       (BoardID,
                        Name,
                        Flags,
                        MinPosts,
                        RankImage)
            VALUES     (@BoardID,
                        @Name,
                        @Flags,
                        @MinPosts,
                        @RankImage);
        END
    END


